package com.woniu40.petai_user.web;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.woniu40.petai_user.config.AlipayConfig;
import com.woniu40.petai_user.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;

@Controller
public class AliPayController {

    @Autowired
    OrderController orderController;

    @Autowired
    CartController cartController;

    @RequestMapping("/pay")
    @ResponseBody
    public String pay(HttpSession session) throws AlipayApiException {

        User user = (User) session.getAttribute("user");
        String userName = user.getUserName();
        //获得订单编号
        String orderNum = (String) session.getAttribute("orderNum");
        //获得订单总价
        BigDecimal totalPriceBig = (BigDecimal) session.getAttribute("totalPrice");
        BigDecimal scale = totalPriceBig.setScale(2, BigDecimal.ROUND_HALF_UP);////设置小数位数，第一个变量是小数位数，第二个变量是取舍方法(四舍五入)
        String totalPrice = scale.toString();//转成字符串

        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id,
                                                            AlipayConfig.merchant_private_key, "json",
                                                            AlipayConfig.charset, AlipayConfig.alipay_public_key,
                                                            AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = orderNum;
        //付款金额，必填
        String total_amount = totalPrice;
        //订单名称，必填
        String subject = userName;
        //商品描述，可空
        String body = "就是有钱，就是任性，就是买买买!!!";

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //输出
        return result;
    }

    //支付宝异步回调地址  需要内网穿透
    /*@RequestMapping("/afterPay")
    @ResponseBody
    public String rturn(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 到支付成功的提示页面
        String orderNum = request.getParameter("out_trade_no");//拿到支付的订单编号
        orderController.update(2,orderNum);
        orderController.removeProduct(orderNum);
        return "success";
    }*/


    //同步回调地址
    @RequestMapping("/afterPay")
    @ResponseBody
    public void rturn(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 到支付成功的提示页面
        String orderNum = request.getParameter("out_trade_no");//拿到支付的订单编号
        response.sendRedirect("http://localhost:9001/goods/show");
        orderController.update(2,orderNum);
        orderController.removeProduct(orderNum);
    }


}
